import { createRouter, createWebHashHistory } from "vue-router";
import Layout from "../views/Layout/Index.vue";
import { useMainStore } from "../pinia/index";

const routerMap = [
  {
    path: "/qlm/index",
    name: "indexHome",
    component: () => import("../views/index.vue"),
  },
  //无线设备管理-设备列表-已国际化
  {
    path: "/wirelessdevice",
    name: "wirelessdevice",
    component: () => import("@/views/workbench/wirelessManage/wirelessdevice.vue"),
  },
  // 设备管理->云平台信息-已国际化
  {
    path: "/toDeviceManager",
    name: "CloudPlatform",
    component: () => import("@/views/workbench/wiredManage/CloudPlatform.vue"),
  },
  // 设备管理->设备列表-已国际化
  {
    path: "/toOperationDeviceManager",
    name: "WiredDeviceList",
    component: () => import("@/views/workbench/wiredManage/WiredDeviceList.vue"),
  },
  // 设备管理->设备分组-已国际化
  {
    path: "/toGroupDeviceManager",
    name: "DeviceGrouping",
    component: () => import("@/views/workbench/wiredManage/DeviceGrouping.vue"),
  },
  // 有线设备管理->设备测试-已国际化
  {
    path: "/test",
    name: "DeviceTest",
    component: () => import("@/views/workbench/wiredManage/DeviceTest.vue"),
  },
  // 设备管理->设备设置-已国际化
  {
    path: "/toSetDeviceManager",
    name: "SetWiredDevice",
    component: () => import("@/views/workbench/wiredManage/SetWiredDevice.vue"),
  },
  // 定时管理->定时任务-已国际化
  {
    path: "/toDeviceTimingRuleManager",
    name: "TimingRuleList",
    component: () => import("@/views/workbench/timedTasks/TimingRuleList.vue"),
  },
  // 电表管理->电表设置-已国际化
  {
    path: "/electricityList",
    name: "ElectricityList",
    component: () => import("@/views/workbench/electricityManage/ElectricityList.vue"),
  },
  // 报警管理->报警列表-已国际化
  {
    path: "/toWarningManager",
    name: "WarningList",
    component: () => import("@/views/workbench/alarmManage/WarningList.vue"),
  },
  // 邮件管理->邮箱配置-已国际化
  {
    path: "/toMailManager",
    name: "MailConfig",
    component: () => import("@/views/workbench/mailManage/MailConfig.vue"),
  },
  // 操作日志->操作日志-已国际化
  {
    path: "/toUserOperation",
    name: "OperationLog",
    component: () => import("@/views/workbench/actionLog/OperationLog.vue"),
  },
  // 系统管理->项目管理-已国际化
  {
    path: "/projectManagement",
    name: "ProjectList",
    component: () => import("@/views/workbench/systemManage/ProjectList.vue"),
  },
  // 系统管理->菜单管理-已国际化
  {
    path: "/toMenuManager",
    name: "MenuList",
    component: () => import("@/views/workbench/systemManage/MenuList.vue"),
  },
  // 系统管理->角色管理-已国际化
  {
    path: "/toLoadAllRole",
    name: "RoleList",
    component: () => import("@/views/workbench/systemManage/RoleList.vue"),
  },
  // 系统管理->用户管理-已国际化
  {
    path: "/toLoadAllUser",
    name: "UserList",
    component: () => import("@/views/workbench/systemManage/UserList.vue"),
  },
  // 系统管理->图标管理-已国际化
  {
    path: "/icon",
    name: "IconList",
    component: () => import("@/views/workbench/systemManage/IconList.vue"),
  },
  // {
  //   path: "/pageSet",
  //   name: "pageSet",
  //   component: () => import("../views/pageSet.vue"),
  // },
  {
    // BETA->VR演示
    path: "/toVR",
    name: "toVR",
    component: () => import("../views/BETA/toVR.vue"),
  },
  {
    path: "/updateLogin",
    name: "updateLogin",
    component: () => import("../views/updateLogin.vue"),
  },
  {
    path: "/setWirelessdevice",
    name: "SetDevice",
    component: () => import("@/views/workbench/wirelessManage/SetDevice.vue"),
  },
];

const routes = [
  {
    path: "/testdemo",
    name: "TestDemo",
    component: () => import("../views/TestDemo.vue"),
  },
  {
    path: "/login",
    name: "Login",
    component: () => import("@/views/Login/Login.vue"),
  },
  {
    path: "/index",
    name: "BigScreen",
    component: () => import("@/views/bigscreen/index.vue"),
  },
  {
    path: "/showroom3d",
    name: "Showroom3D",
    component: () => import("@/views/showroom3D/Showroom3D.vue"),
  },
  {
    path: "/workbench",
    name: "Layout",
    component: Layout,
    redirect: "/workbench",
    children: [
      // 工作台-无线设备列表（已国际化）
      {
        path: "/workbench",
        name: "WirelessDevice",
        component: () => import("@/views/workbench/wirelessManage/WirelessDevice.vue"),
      },
      // 工作台-首页（已国际化）
      {
        path: "/workbench/home",
        name: "WorkbenchHome",
        component: () => import("../views/workbench/home/WorkbenchHome.vue"),
      },
    ],
  },
  {
    path: "/qlm/home",
    name: "home",
    component: () => import("../views/index.vue"),
  },
  {
    path: "/tcc/home",
    name: "home",
    component: () => import("../views/index.vue"),
  },
  {
    path: "/:pathMatch(.*)*",
    component: () => import("@/views/errorpage/404.vue"),
  },
];

const router = createRouter({ history: createWebHashHistory(), routes });

// 在所有组件内守卫和异步路由组件被解析之后触发
router.beforeResolve((to) => {
  if (to.path === "/") {
    router.push("/index");
  }
  try {
  } catch (error) {
    if (error instanceof NotAllowedError) {
      // ... 处理错误，然后取消导航
      return false;
    } else {
      // 意料之外的错误，取消导航并把错误传给全局处理器
      throw error;
    }
  }
});

router.beforeEach((to, from) => {
  if (to.path == "/login" || to.path == "/findUpwd" || to.path == "/approve" || to.path == "/qlm/map" || to.path === "/showroom3d") return true;
  let store = useMainStore();
  let userInfo = JSON.parse(localStorage.getItem("info"));
  // 判断用户是否登录
  if (!userInfo) {
    localStorage.removeItem("info");
    router.push("/login");
    return false;
  }
  if (store.menu == null) {
    store.getMenu(to.path);
    return false;
  }
  // ...
  // 返回 true、false 以取消导航
  // return false
});

export { routes, routerMap };
export default router;
